<!DOCTYPE html>
<html class="um landscape min-width-240px min-width-320px min-width-480px min-width-768px min-width-1024px">
    <head>
        <title></title>
        <meta charset="utf-8">
        <meta name="viewport" content="target-densitydpi=device-dpi, width=device-width, initial-scale=1, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <link rel="stylesheet" href="css/fonts/font-awesome.min.css">
        <link rel="stylesheet" href="css/ui-box.css">
        <link rel="stylesheet" href="css/ui-base.css">
        <link rel="stylesheet" href="css/ui-color.css">
        <link rel="stylesheet" href="css/appcan.icon.css">
        <link rel="stylesheet" href="css/appcan.control.css">
        <link rel="stylesheet" href="info_content/css/main.css">
    </head>
    <body class="um-vp bc-bg" ontouchstart>
        <div class="umar-a" id="">
            <div id="tabview" class="uf sc-bg ubb bc-border" id=""></div>
            <div id="goodsList" class="ub-f1 tx-l"></div>

        </div>
        <script src="js/appcan.js"></script>
        <script src="js/appcan.control.js"></script>
        <script src="js/appcan.listview.js"></script>
        <script src="js/appcan.tab.js"></script>
    </body>
    <script>
        var www = ['http://whynothappy.sinaapp.com/goods_of_a_buyer','http://whynothappy.sinaapp.com/books_of_a_seller'];
        var flag = 0;
        var arrData = [];
        var username = localStorage.getItem("username");
        
        appcan.ready(function() {
            appcan.initBounce();
            if (username == null) username = "1";
            var request = appcan.require('request');
            request.ajax({
                type : 'POST',
                url : 'http://whynothappy.sinaapp.com/goods_of_a_buyer',
                data : {username : username},
                contentType: 'application/json',
                success:function(data){
                    data = eval("(" + data + ")");
                    show(data.books, 0);
                },
                error:function(){
                    alert("can't connect the Internet");
                }
            })
        })
        var tabview = appcan.tab({
            selector : "#tabview",
            hasIcon : false,
            hasAnim : true,
            hasLabel : true,
            hasBadge : false,
            index : 0,
            data : [{
                label : "买的书",
            }, {
                label : "卖的书",
            }]
        });
        tabview.on("click", function(obj, index) {
            var request = appcan.require('request');
            request.ajax({
                type : 'POST',
                url : www[index],
                data : {username : username, seller : username},
                contentType: 'application/json',
                success:function(data){
                    data = eval("(" + data + ")");
                    show(data.books, index);
                },
                error:function(){
                    alert("can't connect the Internet");
                }
            })
        })
        
        
        function show(data, index) {
            
            if (index == 0) {
                if (flag == 1) arrData = [];
                flag = 0;
            }
            else {
                if (flag == 0) arrData = [];
                flag = 1;
            }
            var len = data.length;
            for(var i = 0; i < len; ++i)
            {
                var request = appcan.require('request');
                request.ajax({
                    type : 'POST',
                    url : 'http://whynothappy.sinaapp.com/goodsinfo',
                    data : {username : username , goods_id : data[i].goods_id},
                    contentType: 'application/json',
                    success:function(data){
                        
                        data = eval("(" + data + ")");
                        AddGoods(data, index);
                        //alert("can't connect the Internet");
                    },
                    error:function(){
                        alert("can't connect the Internet");
                    }
                })
            }
            
        }
        function down(x, y) {
            return (x.price > y.price) ? 1 : -1
 
        }
        function AddGoods(data, index) {
            arrData.push({"bookname":data.title, "img":data.pic_url, "price":"￥"+data.price});
            arrData.sort(down);
            var listData = [];
            for (var i = 0,
                len = arrData.length; i < len; i++) {
                    
                var list = {
                    title : arrData[i].bookname,
                    icon : arrData[i].img,
                    describe : '<div class="ub ub-ae"><div class="sc-text-warn ">' + arrData[i].price + '</div></div>',
                }
                listData.push(list);
            }
            var lv1 = appcan.listview({
                selector : "#goodsList",
                type : "thickLine",
                hasIcon : true,
                hasAngle : true,
                hasSubTitle : false,
                multiLine : 1,
                hasCheckbox : false,
                align : 'left'
            });
            lv1.set(listData);
        }
    </script>
</html>
